//package com.tianji.remark.service.impl;
//
//import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
//import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
//import com.tianji.common.constants.MqConstants;
//import com.tianji.common.domain.R;
//import com.tianji.common.utils.StringUtils;
//import com.tianji.common.utils.UserContext;
//import com.tianji.remark.domain.dto.LikeRecordFormDTO;
//import com.tianji.remark.domain.dto.LikedTimesDTO;
//import com.tianji.remark.domain.po.LikedRecord;
//import com.tianji.remark.mapper.LikedRecordMapper;
//import com.tianji.remark.service.ILikedRecordService;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//import java.util.Set;
//import java.util.stream.Collectors;
//
///**
// * <p>
// * 点赞记录表 服务实现类
// * </p>
// *
// * @author mxl
// * @since 2024-06-27
// */
//@Service
//@Slf4j
//@RequiredArgsConstructor
//public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {
//
//    private final RabbitMqHelper rabbitMqHelper;
//
//    @Override
//    public void addLikeRecord(LikeRecordFormDTO likeRecordFormDTO) {
//        // 1. 获取当前登录用户
//        Long user = UserContext.getUser();
//        // 2.判断是否点赞
//        boolean flag = likeRecordFormDTO.getLiked()? like(likeRecordFormDTO, user):  unlike(likeRecordFormDTO, user);
//        if(!flag){
//            return;
//        }
//        // 3 统计总点赞数量
//        Integer count = this.lambdaQuery().eq(LikedRecord::getBizId, likeRecordFormDTO.getBizId()).count();
//        LikedTimesDTO likedTimesDTO = new LikedTimesDTO();
//        likedTimesDTO.setBizId(likedTimesDTO.getBizId());
//        likedTimesDTO.setLikedTimes(count);
//        // 4. 发消息给MQ
//        log.info("发送点赞消息");
//        rabbitMqHelper.send(
//                MqConstants.Exchange.LIKE_RECORD_EXCHANGE,
//                StringUtils.format(MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE, likeRecordFormDTO.getBizType()),
//                likedTimesDTO
//        );
//
//    }
//
//    @Override
//    public Set<Long> getLikesStatusByBizIds(List<Long> bizIds) {
//        // 1. 获取用户id
//        Long user = UserContext.getUser();
//
//        // 2. 查询点赞记录表 in BizIds
//        List<LikedRecord> records = this.lambdaQuery().eq(LikedRecord::getUserId, user).in(LikedRecord::getBizId, bizIds).list();
//
//        // 3. 将查询的业务id返回
//        Set<Long> collect = records.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
//        return collect;
//    }
//
//    private boolean unlike(LikeRecordFormDTO likeRecordFormDTO, Long user) {
//        LikedRecord record = this.lambdaQuery()
//                .eq(LikedRecord::getUserId, user)
//                .eq(LikedRecord::getBizId, likeRecordFormDTO.getBizId()).one();
//
//        if(record == null){
//            // 没有点过赞
//            return false;
//        }
//        boolean isSuccess = this.removeById(record);
//        return isSuccess;
//    }
//
//    private boolean like(LikeRecordFormDTO likeRecordFormDTO, Long user) {
//        LikedRecord record = this.lambdaQuery()
//                .eq(LikedRecord::getUserId, user)
//                .eq(LikedRecord::getBizId, likeRecordFormDTO.getBizId()).one();
//
//        if(record != null){
//            // 已经点赞了
//            return false;
//        }
//        LikedRecord likedRecord = new LikedRecord();
//        likedRecord.setUserId(user);
//        likedRecord.setBizId(likeRecordFormDTO.getBizId());
//        likedRecord.setBizType(likeRecordFormDTO.getBizType());
//        boolean save = save(likedRecord);
//        return save;
//    }
//
//
//}
